package com.shengyuan.manage_os.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.shengyuan.manage_os.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/16.
 */

public class GridViewTools {

    private GridView gview;
    private View view;
    private View view1;
    Context context;
    private List<Map<String, Object>> data_list;
    private List<Map<String, Bitmap>> data_image;
    private SimpleAdapter sim_adapter;
    // 图片封装为一个数组
    private int[] icon = {R.mipmap.my_welfare_icon,
            R.mipmap.icon_org_invite_link,
            R.mipmap.my_favorite_icon,
            R.mipmap.my_service_icon,
            R.mipmap.my_experience_icon,
            R.mipmap.user_my_redpackets_icon,
            R.mipmap.icon_concern};
    private int[] controlIcon = {
            R.mipmap.my_service_icon,
            R.mipmap.icon_org_invite_link,
            R.mipmap.my_experience_icon,
            R.mipmap.icon_concern};
    private String[] iconName = { "日常事务", "工作管理", "日志", "公告", "报销",  "考勤",
            "地图" };
    private String[] controlIconName = { "工程管理", "机械管理" , "行政管理", "网络管理"};

    public GridViewTools(Context context){
        this.context = context;
    }

    public void setGviewData(GridView gridView){
        gview = gridView;
        data_list = new ArrayList<>();
        if(gridView.getId() == R.id.app_item){
            getData(1);
        }else {
            getData(3);
        }
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.text};
        sim_adapter = new SimpleAdapter(context, data_list, R.layout.grid_item, from, to);
        //配置适配器
        gview.setAdapter(sim_adapter);
        gridView.getChildCount();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int arg2, long l) {
                listener.start(arg0.getAdapter(),arg2);
            }
        });

    }

    public List<Map<String, Object>> getData(int tag){
        //cion和iconName的长度是相同的，这里任选其一都可以
        data_list.clear();
        if (tag == 1) {
            for (int i = 0; i < icon.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("image", icon[i]);
                map.put("text", iconName[i]);
                data_list.add(map);
            }
        }else {
            for (int i = 0; i < controlIcon.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("image", controlIcon[i]);
                map.put("text", controlIconName[i]);
                data_list.add(map);
            }
        }

        return data_list;
    }

    private static StartActivityListener listener;
    public static void setStartActivityListener(StartActivityListener listenerp){
        listener = listenerp;
    }
    public interface StartActivityListener{
        void start(Object view,int item);
    }
}
